﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfPaint
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isLine = false;
        private bool isRectangle = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Line_Click(object sender, RoutedEventArgs e)
        {
            isLine = !isLine;
            isRectangle = false;

            ChangeButtonsColor();
        }

        private void Rectangle_Click(object sender, RoutedEventArgs e)
        {
            isRectangle = !isRectangle;
            isLine = false;

            ChangeButtonsColor();
        }

        private void ChangeButtonsColor()
        {
            Rectangle.Background = isRectangle ? Brushes.Red : null;
            Line.Background = isLine ? Brushes.Red : null;
        }

        private Point startPoint;
        private Shape shape;

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(canvas);
            if (isRectangle)
            {
                shape = new Rectangle
                {
                    Stroke = Brushes.LightBlue,
                    StrokeThickness = 2
                };

                Canvas.SetTop(shape, startPoint.X);
                Canvas.SetLeft(shape, startPoint.X);
            }
            if (isLine)
            {
                shape = new Line()
                {
                    X1 = startPoint.X,
                    Y1 = startPoint.Y,
                    Stroke = Brushes.LightBlue,
                    StrokeThickness = 2
                };
            }

            if (shape != null)
            {
                Canvas.SetZIndex(shape, 1);
                canvas.Children.Add(shape);
            }
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released || shape == null)
                return;

            var pos = e.GetPosition(canvas);

            if (isRectangle)
            {
                var x = Math.Min(pos.X, startPoint.X);
                var y = Math.Min(pos.Y, startPoint.Y);

                var w = Math.Max(pos.X, startPoint.X) - x;
                var h = Math.Max(pos.Y, startPoint.Y) - y;

                shape.Width = w;
                shape.Height = h;

                Canvas.SetLeft(shape, x);
                Canvas.SetTop(shape, y);
            }

            if (isLine)
            {
                var line = (Line)shape;
                line.X2 = pos.X;
                line.Y2 = pos.Y;
            }
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            shape = null;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            FileStream fs = File.Open(@"C:\Users\Pavel Lorenz\Desktop\xamlFileName.xaml", FileMode.Create);
            System.Windows.Markup.XamlWriter.Save(canvas, fs);
            fs.Close();
        }

        private void Load_Click(object sender, RoutedEventArgs e)
        {
            FileStream fs = File.Open(@"C:\Users\Pavel Lorenz\Desktop\xamlFileName.xaml", FileMode.Open, FileAccess.Read);
            Canvas savedCanvas = System.Windows.Markup.XamlReader.Load(fs) as Canvas;
            fs.Close();

            Dock.Children.Remove(canvas);
            Dock.Children.Add(savedCanvas);
        }
    }
}
